package asia.huangxi.config;


import asia.huangxi.websocket.interceptor.RbacHandshakeInterceptor;
import asia.huangxi.websocket.socket.WebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;


@Configuration
public class SpringWebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        registry.addHandler(buildTextHandler(), "/ws").setAllowedOrigins("*");
        registry.addHandler(buildTextHandler(), "/ws/container/exec")
                //复制HttpSession中的属性到WebSocketSession中
                .addInterceptors(new RbacHandshakeInterceptor())
                .addInterceptors(new HttpSessionHandshakeInterceptor())
                .setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler buildTextHandler() {
        return new WebSocketHandler();
    }

//    @Bean
//    public WebSocketHandler buildMessageHandler() {
//        return new PersistenceToElasticsearchWebSocketHandlerDecorator(new PersistenceToDatabaseWebSocketHandlerDecorator(new MessageHandler()));
//    }

    /**
     * 扫描注解了@ServerEndpoint注解的类
     */
//    @Bean
//    public ServerEndpointExporter serverEndpointExporter() {
//        return new ServerEndpointExporter();
//    }
}